//
//  XyGradientView.m
//  NBTripe
//
//  Created by Xy on 16/1/8.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "XyGradientView.h"

@implementation XyGradientView


- (id)initWithFrame:(CGRect)frame type:(int) type{
    self = [super initWithFrame:frame];
    if (self) {
        if (type == TRANSPARENT_GRADIENT_TYPE) {
            [self insertTransparentGradient];
        }else if (type == COLOR_GRADIENT_TYPE){
            [self insertColorGradient];
        }
    }
    return self;
    
}

//Transparent Gradient Layer
- (void) insertTransparentGradient {
    UIColor *colorOne = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:0.5];
    UIColor *colorTwo = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1.0];
    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
    NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
    NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];
    NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];
    
    //crate gradient layer
    CAGradientLayer *headerLayer = [CAGradientLayer layer];
    
    headerLayer.colors = colors;
    headerLayer.locations = locations;
    headerLayer.frame = self.bounds;
    
    [self.layer insertSublayer:headerLayer atIndex:0];
}


//color gradient layer
- (void) insertColorGradient {
    
    UIColor *colorOne = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
    UIColor *colorTwo = [UIColor colorWithRed:(33/255.0)  green:(33/255.0)  blue:(33/255.0)  alpha:1.0];
    
    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
    NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
    NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];
    
    NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];
    
    CAGradientLayer *headerLayer = [CAGradientLayer layer];
    headerLayer.colors = colors;
    headerLayer.locations = locations;
    headerLayer.frame = self.bounds;
    
    [self.layer insertSublayer:headerLayer above:0];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
